/* Copyright (c) 2010, Carl Burch. License information is located in the
 * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */

package com.cburch.logisim.gui.generic;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import com.cburch.logisim.gui.generic.ZoomModel;
import com.cburch.logisim.prefs.PrefMonitor;

public class BasicZoomModel implements ZoomModel {
    private double[] zoomOptions;

    private PropertyChangeSupport support;
    private double zoomFactor;
    private boolean showGrid;

    public BasicZoomModel(PrefMonitor<Boolean> gridPref,
            PrefMonitor<Double> zoomPref, double[] zoomOpts) {
        zoomOptions = zoomOpts;
        support = new PropertyChangeSupport(this);
        zoomFactor = 1.0;
        showGrid = true;

        setZoomFactor(zoomPref.get().doubleValue());
        setShowGrid(gridPref.getBoolean());
    }

    @Override
    public void addPropertyChangeListener(String prop, PropertyChangeListener l) {
        support.addPropertyChangeListener(prop, l);
    }

    @Override
    public void removePropertyChangeListener(String prop,
            PropertyChangeListener l) {
        support.removePropertyChangeListener(prop, l);
    }

    @Override
    public boolean getShowGrid() {
        return showGrid;
    }

    @Override
    public double getZoomFactor() {
        return zoomFactor;
    }

    @Override
    public double[] getZoomOptions() {
        return zoomOptions;
    }

    @Override
    public void setShowGrid(boolean value) {
        if (value != showGrid) {
            showGrid = value;
            support.firePropertyChange(ZoomModel.SHOW_GRID, !value, value);
        }
    }

    @Override
    public void setZoomFactor(double value) {
        double oldValue = zoomFactor;
        if (value != oldValue) {
            zoomFactor = value;
            support.firePropertyChange(ZoomModel.ZOOM, Double.valueOf(oldValue),
                    Double.valueOf(value));
        }
    }
}
